﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;

namespace YouHP.BLL.MedicalGraph
{
    public class GraphSymptom : GraphBase
    {

        #region data and param
 
        public GraphSymptom()
        {
            mLegendName = "症状程度";

            this.AddOption("重度","0", System.Drawing.Color.Red);
            this.AddOption("中度","1", System.Drawing.Color.FromArgb(255, 153, 0));
            this.AddOption("轻度","2", System.Drawing.Color.FromArgb(51, 153, 102));
            this.AddOption("无", "3",System.Drawing.Color.White);
        }

        #endregion

        public class ValueOption
        {
            public string mName;

            public string mValue;

            private Color mColor;

            public Brush mBrush;

            public Pen mPen;

            public void SetColor(Color color)
            {
                mColor = color;
                mBrush = new SolidBrush(color);
                mPen = new Pen(color, 2.0f);
            }
        }
        public List<ValueOption> mValueOption = new List<ValueOption>();

        public void AddOption(string name, string value,Color color)
        {
            ValueOption opt = new ValueOption();
            opt.mName = name;
            opt.mValue = value;
            opt.SetColor(color);
            mValueOption.Add(opt);
        }
        public ValueOption GetOption(string value)
        {
            foreach (ValueOption opt in mValueOption)
            {
                if (value == opt.mValue) return opt;
            }
            return null;
        }

        #region SetupGrid
        Dictionary<int, int> mYAxis = new Dictionary<int, int>();
        protected override void SetupAxis()
        {
            int cnt1 = Rule.mGroups.Count;
            int y = _DataRect.Top;
            for (int i = 0; i < cnt1; i++)
            {
                mYAxis.Add(i * 100 + 99, y);
                y += 30;
                int cnt2 = Rule.mGroups[i].mVariables.Count;
                int j = 0;
                for (j = 0; j < cnt2; j++)
                {
                    mYAxis.Add(i * 100 + j, y);
                    y += 30;
                }
                if (i == 0 && j < 5) y += 30 * (5 - j);
            }
        }
        protected override double GetDataY(int index, double y)
        {
            return mYAxis[index];
        }
        protected override double GetDataX(int index, DateTime date)
        {
            return base.GetDataX(index, date);
        }
        #endregion

        #region Draw

        protected override void DrawData()
        {
            
            int j,k;
            int cnt2,cnt3;
            int x1, x2;
            int y1;

            //cnt1 = Rule.;
            Dictionary<int, int> lastx = new Dictionary<int, int>();
            Dictionary<int, Pen> lastp = new Dictionary<int, Pen>();
            //for (i = 1; i <= cnt1; i++)
            //{
            //    //MedicalDataInfo data = Data[i - 1];
            //    //x1 = (int)this.GetDataX(0, i);
            //    //y1 = _DataRect.Bottom + 10;


            cnt2 = Rule.mGroups.Count;
            for (j = 0; j < cnt2; j++)
            {
                GraphRule.Group group = Rule.mGroups[j];
                cnt3 = group.mVariables.Count;

                for (k = 0; k < cnt3; k++)
                {
                    int indexkey = j * 100 + k;
                    if (lastx.ContainsKey(indexkey) == false) lastx.Add(indexkey, -1);

                    GraphRule.Variable var = group.mVariables[k];
                    GraphDataVariableInfo dataVar = Data.GetDataVariable(var.mKey);

                    foreach (GraphDataItemInfo dataItem in dataVar.DataItemList)
                    {
                        ValueOption opt = this.GetOption(dataItem.Value);
                        if (opt != null)
                        {
                            x1 = (int)this.GetDataX(0, dataItem.DateTime1);
                            y1 = (int)this.GetDataY(j * 100 + k, 0);
                            x2 = lastx[indexkey];
                            if (x2 > 0)
                            {
                                _GraphicsData.DrawLine(lastp[indexkey], x1, y1, x2, y1);
                            }
                            _GraphicsData.FillRectangle(opt.mBrush, x1 - 3, y1 - 3, 6, 6);
                            lastp[indexkey] = opt.mPen;
                            lastx[indexkey] = x1;
                        }
                        else
                        {
                            lastx[indexkey] = -1;
                        }
                    }
                }
                //}
            }
             
        }

        protected override void DrawDataGrid()
        {
            int x1, x2;
            int y1, y2;
            int i;
            int count;

            y1 = this._DataRect.Top;
            y2 = this._DataRect.Bottom;
            for (i = 1; i <= _GridCount; i++)
            {
                x1 = (int)this.GetDataX(0, i);
                _GraphicsData.DrawLine(Rule.mGridPen, x1, y1, x1, y2);
                _GraphicsData.DrawString(i.ToString(),
                    Rule.mGridFont, Brushes.Black, x1, y2, Rule.mLabelFotmat);
            }

            for (i = 0; i < MonthCount; i++)
            {
                DateTime dt = _StartDateTime.AddMonths(i);
                x1 = (int)this.GetDataX(0, i+1);
                y1 = _DataRect.Bottom + 10;
                DrawDataTime(dt, x1, y1);
            }

            Rectangle rect = new Rectangle(0, 0, Rule.DataWidth, Rule.Height);
            x1 = rect.Left;
            x2 = rect.Right - 10;
            y1 = rect.Bottom - 30;
            _GraphicsData.DrawLine(Rule.mXAxisPen, x1, y1, x2, y1);

            count = Rule.mGroups.Count;
            for (i = 0; i < count; i++)
            {
                x1 = rect.Left;
                x2 = rect.Right - 10;
                y1 = (int)this.GetDataY(i * 100 + 99, 0);
                _GraphicsData.DrawLine(Rule.mGridPen, x1, y1, x2, y1);
            }

        }

        protected override void DrawLegendGrid()
        {
            int i, j;
            int x1, x2, x3;
            int y1, y2;
            Pen pen1;
            Font font1, font2;
            int cnt1, cnt2;

            cnt1 = Rule.mGroups.Count;
            x1 = 10;
            x2 = Rule.LegendWidth - 5;
            x3 = Rule.LegendWidth;

            StringFormat dformat1 = new StringFormat();
            dformat1.Alignment = StringAlignment.Near;
            dformat1.LineAlignment = StringAlignment.Center;
            StringFormat dformat2 = new StringFormat();
            dformat2.Alignment = StringAlignment.Far;
            dformat2.LineAlignment = StringAlignment.Center;
            font1 = new Font("宋体", 12.0f, FontStyle.Bold);
            pen1 = new Pen(Color.Black, 2.0f);
            font2 = new Font("宋体", 9.0f);
            for (i = 0; i < cnt1; i++)
            {
                GraphRule.Group group = Rule.mGroups[i];
                y1 = (int)this.GetDataY(i * 100 + 99, 0);
                mGraphicsLegend.DrawLine(Style.GridPen, x1, y1, x3, y1);
                mGraphicsLegend.DrawString(group.mName, font1, Brushes.Black, x1 + 10, y1 + 15, dformat1);
                cnt2 = group.mVariables.Count;
                for (j = 0; j < cnt2; j++)
                {
                    GraphRule.Variable var = group.mVariables[j];
                    y1 = (int)this.GetDataY(i * 100 + j, 0);
                    mGraphicsLegend.DrawLine(pen1, x2, y1, x2 + 5, y1);
                    mGraphicsLegend.DrawString(var.mName, font2, Brushes.Black, x2 - 6, y1, dformat2);
                }
            }
            y1 = Rule.Height - 30;
            mGraphicsLegend.DrawLine(pen1, x2, y1, _DataRect.Right, y1);
            y2 = (int)this.GetDataY(99, 0);
            mGraphicsLegend.DrawLine(pen1, x2, y1, x2, y2);
        }

        protected override void DrawLegend()
        {
            int i;
            int x1, x2;
            int y1, y2;
            Pen pen1;
            Font font1;
            Rectangle rect1;
            int cnt1;
            StringFormat dformat;


            Graphics g = this.mGraphicsLegend;
            dformat = new StringFormat();
            dformat.Alignment = StringAlignment.Near;
            dformat.LineAlignment = StringAlignment.Center;
            font1 = new Font("宋体", 9.0f);
            x1 = 40;
            x2 = x1 + 60;
            cnt1 = mValueOption.Count;
            pen1 = new Pen(Color.Black, 1.0f);
            y1 = 70;
            g.DrawRectangle(pen1, x1 - 10, y1, 105, cnt1 * 20 + 35);
            y1 += 10;
            g.DrawString(mLegendName, font1, Brushes.Black, x1, y1);
            y1 += 20;
            for (i = 0; i < cnt1; i++)
            {
                ValueOption option = mValueOption[i];
                y2 = y1 + 10;
                rect1 = new Rectangle(x1, y1, 50, 15);
                g.FillRectangle(option.mBrush, rect1);
                g.DrawRectangle(pen1, rect1);
                g.DrawString(option.mName, font1, Brushes.Black, x2, y2, dformat);
                y1 += 20;

            }

        }

        #endregion

        #region Setup Page
        protected override void SetupPage()
        {
            base.SetupPage();

            //_GridCount = Param.mGridCount > Data.Count ? Param.mGridCount : Data.Count;
            DateTime dt = DateTime.Now;
            string timestr = dt.ToString("yyyyMMddhhmmss");
            mDataPath = Path.Combine(SavePath, string.Format("Sym_{1}_Data_{0}.png", Suffix, timestr));
            mLegendPath = Path.Combine(SavePath, string.Format("Sym_{1}_Legend_{0}.png", Suffix, timestr));
            mWebDataPath = Path.Combine(WebPath, string.Format("Sym_{1}_Data_{0}.png", Suffix, timestr));
            mWebLegendPath = Path.Combine(WebPath, string.Format("Sym_{1}_Legend_{0}.png", Suffix, timestr));
            Result.GraphWebPath += "|" + mWebLegendPath;
            Result.GraphWebPath += "|" + mWebDataPath;

            // 设置
            dt = Data.MinDateTime;
            _StartDateTime = new DateTime(dt.Year, dt.Month, 1);
            dt = Data.MaxDateTime.AddMonths(1);
            _EndDateTime = new DateTime(dt.Year, dt.Month, 1);
            MonthCount = (Data.MaxDateTime.Year - Data.MinDateTime.Year) * 12;
            MonthCount += (Data.MaxDateTime.Month - Data.MinDateTime.Month) + 2;
            _GridCount = MonthCount;
            if (_GridCount < 15) _GridCount = 15;
            Rule.DataWidth = _GridCount * 30 + 30;


            int height = 30;
            int cnt1 = Rule.mGroups.Count;
            for (int i = 0; i < cnt1; i++)
            {
                int cnt2 = Rule.mGroups[i].mVariables.Count;
                int j = 0;
                for (j = 0; j < cnt2; j++)
                {
                    height += 30;
                }
                if (i == 0 && j < 5) height += (5 - j) * 30;
                height += 30;
            }
            height += 60;
            Rule.Height = Rule.Height > height ? Rule.Height : height;

            _DataRect = new Rectangle(0, 30, Rule.DataWidth, Rule.Height - 60);
        }
        #endregion



    }
}
